<?php
/*
* Class teia\webdev\php\core\JTabProvider
* Core class for providing division of content into tabs, with jquery
*
* @author Bernardo Quibiana
* @date created: 20111129
*
* @date modified: 20111129
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

require_once 'DisplayableItem.inc';
require_once 'ITabDataSource.inc';
require_once 'FunctionPool.inc';

class JTabProvider extends DisplayableItem {
	
	private $data_source = null;
	private $per_item_content = false;
	
	public function __construct($id, $name,  $var_array=NULL, $data_source) {
		parent::__construct($id, $name, $var_array);
	
		if ($data_source instanceof ITabDataSource) {
			$this->data_source = $data_source;
		} else {
			throw new \InvalidArgumentException('Invalid class for data source, expected: ITabDataSource', '999');
		}
	}
	
	public function get_tab_num($tab) {
		$tabs = $this->data_source->get_tabs_name();
		$cursor = array_search($tab, $tabs);
		if ($cursor) {
			return $cursor;
		}
		throw (new \InvalidArgumentException('Undefined tab: ' + $tab, 999));
	}
	
	public function display($context='tabs') {
		echo $this->generate($context);
	}
	
	public function generate($context) {
		return $this->generate_page();		
	}
	
	public function generate_page() {
		$tabs = $this->data_source->get_tabs_name();
		$content = $this->generate_tabs($tabs);
		$content .= $this->generate_tab_panes($tabs);
		$content .= $this->generate_tab_activation();
		
		return $content;
	}
	
	protected function generate_tabs($tabs) {
		$content = "<ul class=\"tabs\">\n";
		foreach ($tabs as $tab_name) {
			$content .= "\t<li><a href=\"#\">$tab_name</a></li>\n";
		}
		$content .= "</ul>\n";
		
		return $content;
	}
	
	protected function generate_tab_panes($tabs) {
		$content = "<div class=\"panes\">";
		foreach ($tabs as $tab_name) {
			$tab_content = $this->data_source->get_tab_content();
			$content .= "\t<div>$tab_content</div>\n";
		}
		$content .= "</div>";
		
		return $content;
	}
	
	protected function generate_tab_activation() {
		$content = "
		<script>
			// perform JavaScript after the document is scriptable.
			$(function() {
				// setup ul.tabs to work as tabs for each div directly under div.panes
				$(\"ul.tabs\").tabs(\"div.panes > div\");
			});
		</script>";
		
		return $content;
	}
}

?>